<?php
require_once 'BluesCoders/Loader/ILoader.php';
/**
 * Description of Context
 *
 * @author miho
 */
class BluesCoders_Context {

    protected $_beans;
    protected $_loaders;

    /**
     *
     * @var BluesCoders_Context 
     */
    protected $_parent;

    public function  __construct(BluesCoders_Context $parent = null) {
        $this->_parent = $parent;
    }

    public function registerBean($id, $bean) {
        $this->_beans[$id] = $bean;
    }

    public function getBean($id) {
        if(! isset($this->_beans[$id])) {
            if($this->_parent == null) {
                throw new Exception(sprintf('Bean with id %s does not registered in context', $id));
            }

            return $this->_parent->getBean($id);
        }

        return $this->_beans[$id];
    }

    /**
     *
     * @param string $id
     * @param BluesCoders_Loader_ILoader $loader
     * @return BluesCoders_Context
     */
    public function registerLoader($id, BluesCoders_Loader_ILoader $loader) {
        if(! isset($this->_loaders[$id])) {
            unset ($this->_loaders[$id]);
        }

        $this->_loaders[$id] = $loader;

        return $this;
    }

    /**
     * Return instance of registered loader
     * @param string $id
     * @return BluesCoders_Loader_ILoader
     */
    public function getLoader($id) {
        if(! isset ($this->_loaders[$id])) {
            throw new Exception("loader ".$id.'does not registered');
        }

        return $this->_loaders[$id];
    }


}
?>
